package com.ruoyi.business.listener;

import com.ruoyi.business.config.WebSocketServer;
import com.ruoyi.business.domain.RescueTask;
import com.ruoyi.business.mapper.RescueTaskMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 监听死信队列,如果里面有新消息,进行后续的业务处理
 */
@Component
public class RescueDlxListener {

    @Autowired
    private RescueTaskMapper rescueTaskMapper;

    @RabbitListener(queues = "rescue_dlx_queue")
    public void handleDlxMessage(Message message) throws IOException {
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        Long taskId = (Long) headers.get("taskId");
        if (taskId != null) {
            // 将任务状态改为2-已超时
            RescueTask rescueTask = rescueTaskMapper.selectRescueTaskById(taskId);
            if (rescueTask != null) {
                rescueTask.setStatus(2);
                rescueTaskMapper.updateRescueTask(rescueTask);
                // 更改完订单状态之后通知用户
                WebSocketServer.sendInfo("无人接单已经超时", rescueTask.getUId().toString());
            }
        }
    }
}